package com.example.sparksubmitter.service;

import org.apache.spark.SparkConf;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.junit.Test;

import java.util.Arrays;

public class SparkServiceTest {

    @Test
    public void sparkSqlTest() {
        // 设置 Hadoop 环境变量
        System.setProperty("hadoop.home.dir", "D:\\hadoop-3.3.4");
        System.setProperty("HADOOP_CONF_DIR", "D:\\hadoop_conf");
        System.setProperty("HADOOP_USER_NAME", "hadoop");

        SparkConf sparkConf = new SparkConf()
//                .set("spark.driver.extraJavaOptions", "-Duser.name=hadoop")
//                .set("spark.executor.extraJavaOptions", "-Duser.name=hadoop")
                .set("spark.metrics.enabled", "false");

        // 创建 SparkSession
        SparkSession spark = SparkSession.builder()
                .appName("wangz_SparkTest")
                .master("yarn")
                .config(sparkConf)  // 临时禁用指标系统
                .getOrCreate();

        // 创建测试数据
        Dataset<Row> df = spark.createDataFrame(Arrays.asList(
                new Person(1, "Alice", 30),
                new Person(2, "Bob", 25)
        ), Person.class);

        // 创建临时视图
        df.createOrReplaceTempView("people");

        // 执行 SQL 查询
        Dataset<Row> result = spark.sql("SELECT * FROM people WHERE age > 26");

        // 显示结果
        result.show();

        // 停止 SparkSession
        spark.stop();
    }

    // 辅助类
    public static class Person {
        private int id;
        private String name;
        private int age;

        // 构造函数、Getter 和 Setter
        public Person(int id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        public void setId(int id) {
            this.id = id;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}